#include<bits/stdc++.h>
using namespace std;
int n,q;
int l[505],r[505];
int f[505][505];
int main()
{
    freopen("block.in","r",stdin);
    freopen("block.out","w",stdout);
    cin>>n>>q;
    for(int i=1;i<=q;i++)
    {
        cin>>l[i]>>r[i];
        r[i]--;
    }
    for(int i=1;i<n;i++)
    {
        for(int j=i;j<n;j++)
        {
            int cnt=0;
            for(int k=1;k<=q;k++)
            {
                //cout<<"!!"<<l[k]<<" "<<r[k]<<" ";
                if(i<l[k]&&r[k]<j||(i==l[k]&&r[k]<j)||(i<l[k]&&j==r[k]))
                {
                    //cout<<"case1"<<endl;
                    cnt+=(l[k]-i)+(j-r[k]);
                }
                else if(l[k]<=i&&j<=r[k])
                {
                    //cout<<"case2"<<endl;
                    cnt++;
                }
                else if(l[k]<=j&&l[k]>=i&&r[k]>j)
                {
                    //cout<<"case3"<<endl;
                    cnt+=min(j-l[k]+1,l[k]-i);
                }
                else if(i<=r[k]&&r[k]<=j&&l[k]<i)
                {
                    //cout<<"case4"<<endl;
                    cnt+=min(r[k]-i+1,j-r[k]);
                }
                //else
                //cout<<"case5"<<endl;
            }
            f[i][j]=cnt;
            //cout<<"~~"<<i<<" "<<j<<" "<<f[i][j]<<endl;
        }
    }
    for(int i=1;i<n;i++)
    {
        for(int j=i;j<n;j++)
        {
            for(int k=i;k<j;k++)
            {
                f[i][j]=min(f[i][j],f[i][k]+f[k+1][j]);
            }
        }
    }
    cout<<f[1][n-1];
    return 0;
}
